package net.onewfour.plugin.druid;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.alibaba.druid.pool.DruidDataSource;
import com.jfinal.plugin.IPlugin;
import com.jfinal.plugin.activerecord.IDataSourceProvider;

/**
 * 
 * @author Marc
 * @version 1.0
 */
public class MyDruidPlugin implements IPlugin, IDataSourceProvider {

	private DataSource dataSource = null ; 
	private Logger log = Logger.getLogger(this.getClass());


	public MyDruidPlugin(DataSource ds) {
		dataSource = ds ; 
	}

	@Override
	public DataSource getDataSource() {
		return dataSource;
	}

	@Override
	public boolean start() {
		try {
			((DruidDataSource) dataSource).init();
		} catch (SQLException e) {
			log.error("dataSource coun't Initialization", e);
		}
		((DruidDataSource) dataSource).restart();
		return true;
	}

	
	@Override
	public boolean stop() {
		if(dataSource != null )
			((DruidDataSource)dataSource).close();
		return true;
	}

}
